package com.tangdi.importdata;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.tangdi.Utils;
import com.tangdi.Bean.CZGZ_DWXX;
import com.tangdi.Bean.CZGZ_GZLB;
import com.tangdi.Bean.CZGZ_SJGZ;
import com.tangdi.Bean.CZGZ_ZGGZMX;
import com.tangdi.Dao.impl.GongziInsertImpl;


/**
 * @author yang
 * init salary info
 */
public class Import_GZ_Data {
	public String get_YearMonth(String month){
    	Calendar date =  Calendar.getInstance();
    	
    	SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
    	try {
			Date date2 = dateformat.parse(month);
			date.setTime(date2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	date.add(Calendar.MONTH, -2);
    	return dateformat.format(date.getTime()).substring(0, 6);
	}
	
    public int import_GZMX(String pathName, String month) {
        // 绝对路径或相对路径都可以，写入文件时演示相对路径,读取以上路径的input.txt文件
        //防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw;
        //不关闭文件会导致资源的泄露，读写文件都同理
        //Java7的try-with-resources可以优雅关闭文件，异常时自动关闭文件；详细解读https://stackoverflow.com/a/12665271
        String[] ss = null;
        int rs_delete = -1;
        int rs_insert = -1;
        try{
      		 CZGZ_ZGGZMX gzmx = new CZGZ_ZGGZMX();
      		 Field[] field = gzmx.getClass().getDeclaredFields();
      		 GongziInsertImpl gz_load = new GongziInsertImpl();
      		
      		 rs_delete = gz_load.deleteTableData(month, "CZGZ_ZGGZMX");
      		 if(rs_delete == -1 || rs_delete == -2){
      			 return rs_delete;
      		 }
      		 
        	 InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(pathName)), "UTF-8");
             BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言

             String line;
             
             List<CZGZ_ZGGZMX> gzmx_list = new ArrayList<CZGZ_ZGGZMX>();
             while ((line = br.readLine()) != null) {
                 // 一次读入一行数据
             	ss = line.split(",");

         		CZGZ_ZGGZMX gzmx_1 = new CZGZ_ZGGZMX();
         		
             	for(int i=0; i<ss.length; i++){
             		Utils.setFieldReflect(gzmx_1, CZGZ_ZGGZMX.class, field[i].getName(), ss[i].trim());
             	}
             	gzmx_list.add(gzmx_1);
             }

             br.close();         
             rs_insert = gz_load.addGZMX(gzmx_list, field);
          	 if(rs_insert == -1 || rs_insert == -2){
     			 return rs_insert;
     		 }
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        return 0;
    }
    
    
    public int import_SJGZ(String pathname, String month) {
        String[] ss = null;
        int rs_delete = -1;
        int rs_insert = -1;
        try{
             CZGZ_SJGZ sjgz = new CZGZ_SJGZ();
      		 Field[] field = sjgz.getClass().getDeclaredFields();

      		 GongziInsertImpl gz_load = new GongziInsertImpl();
      		 rs_delete = gz_load.deleteTableData(month, "CZGZ_SJGZ");
      		 if(rs_delete == -1 || rs_delete == -2){
      			 return rs_delete;
      		 }
      		 
        	 InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(pathname)), "UTF-8");
             BufferedReader br = new BufferedReader(reader);      		 
             String line;
             
             List<CZGZ_SJGZ> sjgz_list = new ArrayList<CZGZ_SJGZ>();
             while ((line = br.readLine()) != null) {
                 // 一次读入一行数据
             	ss = line.split(",");
             	CZGZ_SJGZ sjgz_1 = new CZGZ_SJGZ();
             	for(int i=0; i<ss.length; i++){
             		Utils.setFieldReflect(sjgz_1, CZGZ_SJGZ.class, field[i].getName(), ss[i].trim());
             	}
             	sjgz_list.add(sjgz_1);
             }
             br.close();
             rs_insert = gz_load.addSJGZ(sjgz_list, field);
          	if(rs_insert == -1 || rs_insert == -2){
     			 return rs_insert;
     		 }
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        return 0;
    }
    
    
    public int import_GZDWXX(String pathname, String month) {
        String[] ss = null;
        int rs_delete = -1;
        int rs_insert = -1;
        try{
             CZGZ_DWXX gzdwxx = new CZGZ_DWXX();
      		 Field[] field = gzdwxx.getClass().getDeclaredFields();      		 
      		 GongziInsertImpl gz_load = new GongziInsertImpl(); 

      		 rs_delete = gz_load.deleteTableData(month, "CZGZ_DWXX");
      		 if(rs_delete == -1 || rs_delete == -2){
      			 return rs_delete;
      		 }
      		 
        	InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(pathname)), "UTF-8"); 
        	BufferedReader br = new BufferedReader(reader);

             String line;
             List<CZGZ_DWXX> gzdw_list = new  ArrayList<CZGZ_DWXX>();
             while ((line = br.readLine()) != null) {
             	ss = line.split(",");

             	CZGZ_DWXX gzdw_1 = new CZGZ_DWXX();
         		
             	for(int i=0; i<ss.length; i++){
             		Utils.setFieldReflect(gzdw_1, CZGZ_DWXX.class, field[i].getName(), ss[i].trim());
             	}
             	gzdw_list.add(gzdw_1);
             }

            br.close();
          	rs_insert = gz_load.addGZDWXX(gzdw_list, field);
          	if(rs_insert == -1 || rs_insert == -2){
     			 return rs_insert;
     		 }
          	
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        return 0;
    }
    
    
    public int import_GZLB(String pathname, String month) {
        String[] ss = null;
        int rs_delete = -1;
        int rs_insert = -1;
        try{
             CZGZ_GZLB gzlb = new CZGZ_GZLB();
      		 Field[] field = gzlb.getClass().getDeclaredFields();
      		 
      		 GongziInsertImpl gz_load = new GongziInsertImpl();

      		 rs_delete = gz_load.deleteTableData(month, "CZGZ_GZLB");
      		 if(rs_delete == -1 || rs_delete == -2){
      			 return rs_delete;
      		 }
      		 
        	 InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(pathname)), "UTF-8");
        	 BufferedReader br = new BufferedReader(reader);
             String line;
             List<CZGZ_GZLB> gzlb_list = new  ArrayList<CZGZ_GZLB>();
             while ((line = br.readLine()) != null) {
             	ss = line.split(",");

             	CZGZ_GZLB gzlb_1 = new CZGZ_GZLB();
         		
             	for(int i=0; i<ss.length; i++){
             		Utils.setFieldReflect(gzlb_1, CZGZ_GZLB.class, field[i].getName(), ss[i].trim());
             	}
             	gzlb_list.add(gzlb_1);
             }
            br.close(); 
          	rs_insert = gz_load.addGZLB(gzlb_list, field);
          	if(rs_insert == -1 || rs_insert == -2){
      			 return rs_insert;
      		 }
          	
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        return 0;
    }    
/*    public static void main(String[] args){
    	Import_GZ_Data ss = new Import_GZ_Data();
    	int i = ss.import_GZDWXX("D:\\桌面文件\\参与项目\\南京交行数据打印系统\\import_data\\201905_DWXX.txt", "201905");
    	System.out.println(i);
    	Calendar date =  Calendar.getInstance();
    	SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
    	date.add(Calendar.MONTH, -3);
    	System.out.println(dateformat.format(date.getTime()).substring(0, 6));
    	System.out.println(ss.get_YearMonth());
    	
    }*/
}
